package com.att.action;

import com.adkfp.utils.LayuiUtil;
import com.att.bean.CheckInOutBean;
import com.att.bean.ParamBean;
import com.att.service.AccessLogService;
import com.att.service.CheckInOutService;
import com.att.service.ParamService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("checkInOut")
public class CheckInOutAction {

    @Resource(name = "checkInOutService")
    private CheckInOutService checkInOutService;
    @Resource(name = "paramService")
    private ParamService paramService;
    @Resource(name = "accessLogService")
    private AccessLogService accessLogService;

    /**
     * Description: 带条件查询补勤记录
     *
     * @param bean
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll(CheckInOutBean bean) throws ParseException {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        SimpleDateFormat format2 = new SimpleDateFormat("HH:mm:ss");
        DateFormat df3 = DateFormat.getTimeInstance();
        ParamBean paramBean = new ParamBean();
        List<ParamBean> paramBeanList = paramService.findByCondition(paramBean);
        List<CheckInOutBean> list = checkInOutService.findByCondition(bean);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getCheckTime() != null && list.get(i).getCheckInTime1() != null && list.get(i).getCheckOutTime2() !=null){
                Date checkTime = format2.parse(df3.format(list.get(i).getCheckTime()));
                Date checkInTime1 = format2.parse(df3.format(list.get(i).getCheckInTime1()));
                Date checkInTime2 = format2.parse(df3.format(list.get(i).getCheckInTime2()));
                Date checkOutTime1 = format2.parse(df3.format(list.get(i).getCheckOutTime1()));
                Date checkOutTime2 = format2.parse(df3.format(list.get(i).getCheckOutTime2()));
                if(list.get(i).getStartTime() != null && list.get(i).getEndTime() != null  && list.get(i).getAutoSchPlan() != null){
                    Date startTime = list.get(i).getStartTime();
                    Date endTime = list.get(i).getEndTime();
                    Date checkTime2 = list.get(i).getCheckTime();
                    if((checkTime2.before(startTime) || checkTime2.after(endTime)) && list.get(i).getAutoSchPlan() == 0){
                        list.get(i).setOtherStatus(2);//无效
                    }
                    else if(list.get(i).getCheckType() != null){
                        if(list.get(i).getCheckType().equals("4") || list.get(i).getCheckType().equals("5") || list.get(i).getCheckType().equals("4") || list.get(i).getCheckType().equals("5")){
                            list.get(i).setOtherStatus(4);//加班
                        }
                        if(list.get(i).getCheckType().equals("2") || list.get(i).getCheckType().equals("3") || list.get(i).getCheckType().equals("2") || list.get(i).getCheckType().equals("3")){
                            list.get(i).setOtherStatus(5);//外出
                        }
                        if(list.get(i).getCheckType().equals("0")){
                            if((checkTime.compareTo(checkInTime1) == 0 || checkTime.after(checkInTime1)) && (checkTime.compareTo(checkInTime2) == 0 || checkTime.before(checkInTime2))){
                                list.get(i).setOtherStatus(1);//正常记录
                            }else {
                                list.get(i).setOtherStatus(2);//无效
                            }
                        }
                        if(list.get(i).getCheckType().equals("1")){
                            if((checkTime.compareTo(checkOutTime1) == 0 || checkTime.after(checkOutTime1)) && (checkTime.compareTo(checkOutTime2) == 0 || checkTime.before(checkOutTime2))){
                                list.get(i).setOtherStatus(1);//正常记录
                            }else {
                                list.get(i).setOtherStatus(2);//无效
                            }
                        }
                    }
                    else if(checkTime.before(checkInTime1) || checkTime.after(checkOutTime2)){
                        list.get(i).setOtherStatus(2);//无效
                    }
                    else if(list.get(i).getCheckTime().before(list.get(i).getStartTime()) || list.get(i).getCheckTime().after(list.get(i).getEndTime())){
                        list.get(i).setOtherStatus(3);//自由加班
                    }
                    else if(list.get(i).getCheckTime().before(list.get(i).getStartTime()) || list.get(i).getCheckTime().after(list.get(i).getEndTime()) && list.get(i).getAutoSchPlan() == 1){
                        list.get(i).setOtherStatus(6);//智能排班
                    }
                    else if(list.get(i).getCheckType() == null){
                        if((checkTime2.after(startTime) && checkTime2.before(endTime))){
                            if(checkTime.after(checkInTime1) && checkTime.before(checkInTime2)){
                                list.get(i).setCheckType("0");
                                list.get(i).setOtherStatus(1);//正常
                            }
                            if(checkTime.after(checkOutTime1) && checkTime.before(checkOutTime2)){
                                list.get(i).setCheckType("1");
                                list.get(i).setOtherStatus(1);//正常
                            }
                        }
                    }
                    else {
                        list.get(i).setOtherStatus(1);//正常
                    }
                    if (i > 0) {
                        if (list.get(i).getUserID().equals(list.get(i - 1)
                                .getUserID())) {
                            Date checkTime1 = list.get(i).getCheckTime();
                            Date checkTime3 = list.get(i - 1).getCheckTime();
                            if(checkTime1.equals(checkTime3)){
                                list.get(i).setOtherStatus(7);//重复
                            }
                            else if(checkTime1.after(checkTime3)){
                                long min = Math.abs(list.get(i).getCheckTime().getTime() - list.get(i - 1).getCheckTime().getTime()) / 1000 /60;//diff % nd % nh / nm
                                if(min<paramBeanList.get(0).getEffectInterval()){
                                    list.get(i).setOtherStatus(7);//重复
                                }
                            }else  if(checkTime3.after(checkTime1)){
                                long min = Math.abs(list.get(i).getCheckTime().getTime() - list.get(i - 1).getCheckTime().getTime()) / 1000 /60;
                                if(min<paramBeanList.get(0).getEffectInterval()){
                                    list.get(i).setOtherStatus(7);//重复
                                }
                            }
                        }
                    }
                }
            }
        }
        List<CheckInOutBean> count = checkInOutService.findByConditionCount(bean);
        return LayuiUtil.data(Long.valueOf(count.size()), list);
    }

    /**
     * Description: 转发到页面
     */
    @RequestMapping("checkInOutIndex")
    public String checkInOutIndex() {
        return "check/checkInOut";
    }

    //新增
    @RequestMapping("insert")
    @ResponseBody
    @Transactional
    public String insert(CheckInOutBean bean) {
        String[] idList = bean.getUserID().split(",");
        for (int j = 0; j < idList.length; j++) {
            String id = idList[j];
            bean.setUserID(id);
            boolean flag = checkInOutService.insert(bean);
            if (!flag) {
                return "ERROR";
            }
        }
        return "SUCCESS";
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public String update(CheckInOutBean bean) {
        boolean flag = checkInOutService.update(bean);
        if (flag) {
            return "SUCCESS";
        }
        return "ERROR";
    }

    //修改
    @RequestMapping("delete")
    @ResponseBody
    public String delete(CheckInOutBean bean) {
        Integer id = bean.getId();
        if(bean.getCheckType() != null && !bean.getCheckType().equals("")){
            boolean flag = checkInOutService.delete(id);
            if (!flag) {
                return "ERROR";
            }
        }else {
            boolean flag = accessLogService.delete(id);
            if (!flag) {
                return "ERROR";
            }
        }
        return "SUCCESS";
    }
}
